cmake_minimum_required(VERSION 3.5)

project(sfm LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set (PSP_VERSION_MAJOR 1)
option(USE_EIGEN3 "USE Eigen3 Library" OFF)
option(USE_PYTHON "USE Python Library" OFF)
option(USE_BOOST "USE BOOST Library" OFF)
if(USE_EIGEN3)
    find_package(Eigen3 REQUIRED)
    set(USE_EIGEN3_LIB 1)
else()
    set(USE_EIGEN3_LIB 0)
endif()
if(USE_BOOST OR USE_PYTHON) # USE PYTHON depedens on USE_BOOST
    find_package(Boost REQUIRED)
    set(USE_BOOST_INCLUDE 1)
else()
    set(USE_BOOST_INCLUDE 0)
endif()
if(WIN32)
    set(DYNAMIC_LIB_SUFFIX dll)
else()
    set(DYNAMIC_LIB_SUFFIX so)
endif()

configure_file (
  "${PROJECT_SOURCE_DIR}/core/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
  )

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
if(USE_EIGEN3)    
    if(UNIX)
    include_directories(${EIGEN3_INCLUDE_DIR})
    endif()
endif()

add_executable(main main.cpp)
if(USE_PYTHON)
    find_package(PythonLibs)
    find_package(PythonInterp)
    if(WIN32)
    find_package(Boost REQUIRED python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
    else()
    find_package(Boost REQUIRED python-py${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})    
    endif()
    add_library(psp SHARED ${PROJECT_SOURCE_DIR}/core/python/psp.cpp)
    target_include_directories(psp PUBLIC ${PYTHON_INCLUDE_DIR})
    target_include_directories(psp PUBLIC ${Boost_INCLUDE_DIRS})
    
    if(WIN32)
    target_link_libraries(psp debug ${Boost_PYTHON${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}_LIBRARY_DEBUG})    
    target_link_libraries(psp optimized ${Boost_PYTHON${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}_LIBRARY_RELEASE})
    else()
    target_link_libraries(psp ${Boost_PYTHON-PY${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}_LIBRARY_RELEASE})
    endif()
    # magic files to extract the release library
    
    if(WIN32)
    string(REPLACE "debug" ";" PYTHON_LIBRARY ${PYTHON_LIBRARY})
    list(GET PYTHON_LIBRARY 0 PYTHON_LIBRARY)
    string(REPLACE "optimized" ";" PYTHON_LIBRARY ${PYTHON_LIBRARY})
    list(GET PYTHON_LIBRARY 1 PYTHON_RELEASE_LIBRARY)
    # we don't link the debug build with Python debug lib, since we are not interested in python core debug info.
    target_link_libraries(psp ${PYTHON_RELEASE_LIBRARY}) 
    else()
    target_link_libraries(psp ${PYTHON_LIBRARY}) 
    endif()
    
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import site;print(site.getsitepackages()[-1],end='')" OUTPUT_VARIABLE PYTHON_SIDE_PACKAGES)
    if(WIN32)
        if(EXISTS "${PROJECT_BINARY_DIR}/Debug/psp.${DYNAMIC_LIB_SUFFIX}")
            set(INSTALL_TYPE Debug)
        else()
            set(INSTALL_TYPE Release)
        endif()
        install(FILES ${PROJECT_BINARY_DIR}/${INSTALL_TYPE}/psp.${DYNAMIC_LIB_SUFFIX} DESTINATION ${PYTHON_SIDE_PACKAGES} RENAME psp.pyd)
    else()
        install(FILES ${PROJECT_BINARY_DIR}/libpsp.${DYNAMIC_LIB_SUFFIX} DESTINATION ${PYTHON_SIDE_PACKAGES} RENAME psp.${DYNAMIC_LIB_SUFFIX})
    endif()
endif()
option(ENABLE_TESTING "Enable testing" ON)
if(ENABLE_TESTING)
  find_package(GTest REQUIRED)
  include(CTest)
  add_subdirectory(test)
endif()
